#pragma once
#include <sstream>
#include <cstddef>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <odb/nullable.hxx>
#include <odb/core.hxx>

#pragma db object
class Student
{
public:
    Student() {}
    Student(unsigned long student_id, std::string name, unsigned short age, unsigned long class_id) : _student_id(student_id), _name(name), _age(age), _class_id(class_id) {}
    unsigned long get_id() const { return _id; }
    unsigned long get_student_id() const { return _student_id; }
    std::string get_name() const { return _name; }
    odb::nullable<unsigned short> get_age() const { return _age; }
    unsigned long get_class_id() const { return _class_id; }
    void set_student_id(unsigned long student_id) { _student_id = student_id; }
    void set_name(std::string name) { _name = name; }
    void set_age(unsigned short age) { _age = age; }
    void set_class_id(unsigned short class_id) { _class_id = class_id; }

private:
    friend class odb::access;
#pragma db id auto
    unsigned long _id;
#pragma db unique
    unsigned long _student_id; // 学号
    std::string _name;
    odb::nullable<unsigned short> _age;
#pragma db index
    unsigned long _class_id;
};

#pragma db object
class Classes
{
public:
    Classes() {}
    Classes(std::string name) : _name(name) {}
    std::string get_name() const { return _name; }
    void set_name(std::string name) { _name = name; }

private:
    friend class odb::access;
#pragma db id auto
    unsigned long _id;
    std::string _name;
};

// 查询所有的学生信息,并显示班级名称
#pragma db view object(Student) object(Classes = classes : Student::_class_id == classes::_id) query((?))
struct Student_Classes
{
#pragma db column(Student::_id)
    unsigned long id;
#pragma db column(Student::_student_id)
    unsigned long student_id;
#pragma db column(Student::_name)
    std::string student_name;
#pragma db column(Student::_age)
    odb::nullable<unsigned short> age;
#pragma db column(classes::_name)
    std::string classes_name;
};

// 只查询学生姓名, (?) 外部调用时传入过滤条件
#pragma db view query("select name from Student")
struct all_student_name
{
    std::string name;
};

// odb -d mysql --std c++11 --generate-query --generate-schema --profile boost/date-time student.hxx